from flask import Flask, render_template, request, flash, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, Column, Integer, Text, DateTime, SMALLINT, Float, Boolean
app = Flask(__name__,instance_relative_config=True,template_folder='templates')
app.config['SECRET_KEY'] = "random string"
HOSTNAME = "127.0.0.1"
PORT = 3306
USERNAME = "root"
PASSWORD = "root"
DATABASE = "flask_games"
# 拼接式创建
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4"
# 每次执行会连接到 flask库
db = SQLAlchemy(app)


# normal  删除destroy
class User(db.Model):
    __tablename__  = "a_users"
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    user_id = db.Column(db.String(32),nullable=False)
    nickName = db.Column(db.String(32),nullable=True, comment='昵称')
    password = db.Column(db.String(32),nullable=False, comment='密码')

    account = db.Column(db.String(32),nullable=False, comment='账号')
    realName = db.Column(db.String(32),nullable=True, comment='真实姓名')
    head_img = db.Column(db.Text,nullable=True, comment='用户头像')
    company_config = db.Column(db.Text,nullable=True, comment='店铺设置')
    storeConfig = db.Column(db.Text,nullable=True, comment='储存设置')
    wx_config = db.Column(db.Text,nullable=True, comment='微信小程序配置') #
    theme_config = db.Column(db.Text,nullable=True, comment='主题设置') #
    phone = db.Column(db.String(32),nullable=True, comment='手机号码') #
    register_date = db.Column(db.DateTime,nullable=True, comment='注册时间')
    type = db.Column(db.String(32),nullable=True, comment='账号类型(a,b,c)') # a代表a端  b代表b端
    template_id = db.Column(db.DateTime,nullable=True, comment='模版id') # 模版id
    USER_SERVICE_TOKEN = db.Column(db.Text,nullable=True, comment='服务标识') # 小程序通过服务标识来确定小程序数据
    passwordstr = db.Column(db.String(32), nullable=True, comment='显示密码')  # 显示密码
    plugin_id = db.Column(db.Text, nullable=True, comment='显示密码')  # 显示密码
    status = db.Column(db.String(32),nullable=True,comment='状态') # normal  删除destroy
#
#  token
class Token(db.Model):
    __tablename__ = "token"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    token = db.Column(db.Text, nullable=False, comment='token登录校验')
    user_id = db.Column(db.String(32), nullable=False, comment='用户id')
    update_date = db.Column(db.DateTime, nullable=False, comment='修改时间')
    login_date = db.Column(db.DateTime, nullable=False, comment='登录时间')
    timestamp = db.Column(db.Text, nullable=False, comment='登录失效时间') # 60秒     按秒计算   60*60*24=一天
# # 角色
class Authority_roles(db.Model):
    __tablename__ = "authority_roles"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(32), nullable=False, comment='角色属性')
    name = db.Column(db.String(32), nullable=False, comment='角色名称')
    menu_list = db.Column(db.Text, nullable=True, comment='角色菜单权限')
    ctime = db.Column(db.DateTime, nullable=False, comment='添加时间')
#
# # 菜单
class Authority_menus(db.Model):
    __tablename__ = "authority_menus"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(255), nullable=False, comment='菜单名称')
    menuType = db.Column(db.Integer, nullable=True, comment='菜单类型')
    name = db.Column(db.String(255), nullable=False, comment='路由名称')
    icon = db.Column(db.String(255), nullable=True, comment='菜单icon')
    extraIcon = db.Column(db.String(255), nullable=True, comment='右侧图标')
    component = db.Column(db.String(255), nullable=True, comment='组件路径')
    menuRank = db.Column(db.Integer, nullable=True, comment='排序')
    enterTransition = db.Column(db.Text, nullable=True, comment='进场动画')
    leaveTransition = db.Column(db.Text, nullable=True, comment='离场动画')
    activePath = db.Column(db.Text, nullable=True, comment='菜单激活')
    hiddenTag = db.Column(db.Text, nullable=True, comment='标签页')
    parentId = db.Column(db.parentId, nullable=True, comment='(父=0 子=父id)')
    path = db.Column(db.Text, nullable=True, comment='菜单路径')
    auths = db.Column(db.Text, nullable=True, comment='权限标识')
    frameLoading = db.Column(db.Text, nullable=True, comment='加载动画')
    showParent = db.Column(db.Text, nullable=True, comment='父级菜单（是否显示父级菜单')
    frameSrc = db.Column(db.Text, nullable=True, comment='链接地址')
    redirect = db.Column(db.Text, nullable=True, comment='菜单重定向')
    fixedTag = db.Column(db.Text, nullable=True, comment='固定标签页')
    keepAlive = db.Column(db.Text, nullable=True, comment='缓存页面')
    showLink = db.Column(db.Integer, nullable=True, comment='菜单隐藏显示')
    uid = db.Column(db.Integer, nullable=True, comment='菜单级别') # uid=0是一级菜单    当uid=存在的id =>  此菜单是id的二级菜单
    ctime = db.Column(db.DateTime, nullable=False, comment='添加时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 用户角色中间表
class Auser_roles(db.Model):
    __tablename__ = "ausers_roles"
    id = db.Column(db.Integer, primary_key=True,  autoincrement=True)
    user_id = db.Column(db.String(32), nullable=False, comment='用户id')
    role_id = db.Column(db.String(32), nullable=True, comment='角色id')
    menu_list = db.Column(db.Text, nullable=True, comment='角色菜单权限')
    ctime = db.Column(db.DateTime, nullable=False, comment='添加时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')  # normal  删除destroy

# c端用户
class C_Users(db.Model):
    __tablename__ = "c_users"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='分组名称', nullable=False)
    openid = db.Column(db.String(64),comment='用户openid',nullable=False)
    nickName = db.Column(db.String(64),comment='昵称',nullable=True)
    age = db.Column(db.String(64),comment='用户年龄',nullable=True)
    gender = db.Column(db.String(64),comment='性别',nullable=True)
    head_img = db.Column(db.Text,comment='用户头像',nullable=True)
    phone = db.Column(db.Integer,comment='手机号码', nullable=True)
    config = db.Column(db.Text,comment='用户配置', nullable=True)
    create_time = db.Column(db.DateTime, nullable=False, comment='注册时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')



# 文件管理图片
class FileManage(db.Model):
    __tablename__ = "File_Manage"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    url = db.Column(db.String(255),comment='地址',nullable=False)
    suffix = db.Column(db.String(64),comment='后缀',nullable=True)
    type = db.Column(db.String(64),comment='分类',nullable=True)
    file_manage_type = db.Column(db.String(64),comment='分组',nullable=True)
    other = db.Column(db.Text,comment='其他操作', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='上传时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 文件管理分组
class FileManage_group(db.Model):
    __tablename__ = "FileManage_group"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255),comment='分组名称',nullable=False)
    name = db.Column(db.String(255),comment='分组名称',nullable=False)
    type = db.Column(db.String(64),comment='分类',nullable=True)
    other = db.Column(db.Text,comment='其他操作', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='上传时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')
# 商品
class Goods(db.Model):
    __tablename__ = "goods"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    distributionFee = db.Column(db.String(255), comment='配送费', nullable=True)
    effectDays = db.Column(db.String(255), comment='购买有效日期', nullable=True)
    examineStatus = db.Column(db.String(32), comment='审核状态：0-待审核 1-审核成功 2-审核失败', nullable=True)
    extJson = db.Column(db.Text, comment='扩展JSON', nullable=True)
    categoryId = db.Column(db.Text, comment='分类id(1,2,3格式 分隔符)', nullable=True)
    goodsBeginTime = db.Column(db.DateTime, comment='商品有效期开始时间(上架时间)', nullable=True)
    goodsEndingTime = db.Column(db.DateTime, comment='商品有效期结束时间(下架时间)', nullable=True)
    goodsDetails = db.Column(db.Text, comment='商品详情', nullable=True)
    goodsDetailsJson = db.Column(db.Text, comment='商品描述', nullable=True)
    goodsName = db.Column(db.String(64), comment='商品名称', nullable=True)
    goodsSkuJson = db.Column(db.Text, comment='商品规格', nullable=True)
    goodsPrice = db.Column(db.String(32), comment='商品价格', nullable=True)
    goodsStatus = db.Column(db.String(32), comment='0正常 1下架', nullable=True)
    goodsType = db.Column(db.String(32), comment='1-快递,2-自提,3-到店消费,4快递自提都支持 5全部支持', nullable=True)
    goodsViewCount = db.Column(db.String(32), comment='浏览量', nullable=True)
    goodsWeight = db.Column(db.String(32), comment='商品重量单位克', nullable=True)
    limitNum = db.Column(db.String(32), comment='限购数量', nullable=True)
    memberPrice = db.Column(db.String(32), comment='会员价', nullable=True)
    packingFee = db.Column(db.String(32), comment='打包费', nullable=True)
    previewUrl = db.Column(db.String(32), comment='商品预览图(列表图)', nullable=True)
    retirementRule = db.Column(db.String(255), comment='退改规则', nullable=True)
    rewardIntegral = db.Column(db.String(32), comment='下单可获得积分', nullable=True)
    rotationImgsJson = db.Column(db.Text, comment='商品详情轮播图', nullable=True)
    scribePrice = db.Column(db.String(32), comment='商品划线价', nullable=True)
    shareJson = db.Column(db.String(32), comment='分享信息JSON', nullable=True)
    stockNum = db.Column(db.String(32), comment='库存量', nullable=True)
    contents = db.Column(db.Text, comment='商品说明(富文本)', nullable=True)
    subtitle = db.Column(db.String(255), comment='副标题', nullable=True)
    keywords = db.Column(db.String(255), comment='关键词(多个关键词以逗号分隔)', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 商品分类
class Category(db.Model):
    __tablename__ = "category"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    businessId = db.Column(db.Text, comment='店铺ID', nullable=True)
    categoryDesc = db.Column(db.Text, comment='分类说明', nullable=True)
    categoryName = db.Column(db.Text, comment='分类名称', nullable=True)
    image = db.Column(db.Text, comment='图片链接', nullable=True)
    pid = db.Column(db.Text, comment='父id', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')

# 订单表
class order(db.Model):
    __tablename__ = "order"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    order_id = db.Column(db.String(255), comment='订单id', nullable=True)
    business_id = db.Column(db.String(255), comment='店铺id', nullable=True)
    wechat_openid = db.Column(db.String(255), comment='微信用户openid', nullable=True)
    wechatNickname = db.Column(db.String(255), comment='微信用户名称', nullable=True)
    wechatavatar = db.Column(db.String(255), comment='用户头像', nullable=True)
    wechatphone = db.Column(db.String(255), comment='用户手机号码', nullable=True)
    goods_id = db.Column(db.Text, comment='商品id(分隔符  1,2,3)', nullable=True)
    goods_sku = db.Column(db.Text, comment='商品规格', nullable=True)
    order_type = db.Column(db.Text, comment='订单类型，1普通订单，2月卡订单，3补录订单', nullable=True)
    paypal_type = db.Column(db.Text, comment='支付类型，1微信支付、2支付宝支付、3余额支付、4月卡支付', nullable=True)
    distribution_type = db.Column(db.Text, comment='配送方式', nullable=True)
    total_money = db.Column(db.Text, comment='订单实际支付金额', nullable=True)
    goods_money = db.Column(db.Text, comment='订单商品需支付金额', nullable=True)
    return_money = db.Column(db.Text, comment='累计退款金额，默认0', nullable=True)
    utime = db.Column(db.DateTime, nullable=False, comment='修改时间')
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')


# 交易货币记录
class currencyRecord(db.Model):
    __tablename__ = "currency_record"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    wechat_openid = db.Column(db.String(255), comment='微信用户openid', nullable=True)
    payment_number = db.Column(db.String(255), comment='支付单号', nullable=True)
    currencyNumber = db.Column(db.Integer, comment='充值金额', nullable=True)
    payment_cash = db.Column(db.Integer, comment='支付金额', nullable=True)
    give_currencyNumber = db.Column(db.Integer, comment='赠送金额', nullable=True)
    payment_type = db.Column(db.String(255), comment='支付方式', nullable=True)
    channel = db.Column(db.String(255), comment='充值渠道', nullable=True)
    explain = db.Column(db.Text, comment='说明', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')


# 订单记录
class OrderRecord(db.Model):
    __tablename__ = "OrderRecord"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # False不能为空   True可以为空
    merchant_id = db.Column(db.String(255), comment='商家id', nullable=False)
    wechat_openid = db.Column(db.String(255), comment='微信用户openid', nullable=True)
    payment_number = db.Column(db.String(255), comment='支付单号', nullable=True)
    order_id = db.Column(db.String(255), comment='订单id', nullable=True)
    OrderCash = db.Column(db.String(255), comment='订单金额', nullable=True)
    Text = db.Column(db.Text, nullable=False, comment='富文本')
    type = db.Column(db.String(255), nullable=False, comment='订单类型')
    JSONData = db.Column(db.Text, comment='json数据', nullable=False)
    access = db.Column(db.String(255), comment='获取方式', nullable=True)
    explain = db.Column(db.Text, comment='说明', nullable=True)
    ctime = db.Column(db.DateTime, nullable=False, comment='创建时间')
    status = db.Column(db.String(32), nullable=True, comment='状态')


with app.app_context():
    db.create_all()